<template>
    <div>
        <div style="margin: 10px 0 ">
            <el-input style="width: 200px" placeholder="请输入商品编号" suffix-icon="el-icon-search" v-model="id"></el-input>
            <el-input style="width: 200px" placeholder="请输入商品名称" suffix-icon="el-icon-message" class="ml-5" v-model="name"></el-input>
            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>
        <div style="margin: 10px 0 ">
            <el-button type="primary"@click="save">新增<i class="el-icon-circle-plus-outline"></i></el-button>

<!--            <el-upload action="http://localhost:9999/user/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">-->
<!--                <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>-->
<!--            </el-upload>-->
<!--            <el-button type="primary" @click="exp" class="ml-5">导出<i class="el-icon-upload2"></i></el-button>-->

        </div>
        <el-table style="width: 100%" :data="tableData" header-cell-class-name="headerBg" @selection-change="handleSelectionChange">

            <el-table-column type="expand" >
                <template slot-scope="props">
                    <el-form label-position="left" inline class="demo-table-expand">
                        <el-form-item label="商品名称">
                            <span>{{ props.row.name }}</span>
                        </el-form-item>
                        <el-form-item label="所属店铺">
                            <span>{{ props.row.firm }}</span>
                        </el-form-item>
                        <el-form-item label="商品 ID">
                            <span>{{ props.row.id }}</span>
                        </el-form-item>
                        <el-form-item label="商品价格">
                            <span>{{ props.row.price }}</span>
                        </el-form-item>
                        <el-form-item label="店铺地址">
                            <span>{{ props.row.address }}</span>
                        </el-form-item>
                        <el-form-item label="商品描述">
                            <span>{{ props.row.introduce }}</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="商品名称" width="140">
            </el-table-column>
            <el-table-column prop="firm" label="店铺名称" width="140">
        </el-table-column>
            <el-table-column prop="number" label="库存" width="120">
            </el-table-column>
            <el-table-column>
                <template slot-scope="scope">
                    <el-button type="success" @click="push(scope.row)" >发布<i class="ml-5 el-icon-edit"></i></el-button>
                    <el-button type="success" @click="edit(scope.row)" >编辑<i class="ml-5 el-icon-edit"></i></el-button>
                    <el-popconfirm
                            confirm-button-text='好的'
                            cancel-button-text='不用了'
                            icon="el-icon-info"
                            icon-color="red"
                            title="这是一段内容确定删除吗？"
                            @confirm="del(scope.row.id)"
                    >
                        <el-button class="ml-5" type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <div style="padding: 10px 0 ">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[4,6, 8, 12]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
        <!-- Form -->
        <el-dialog title="新增商品" :visible.sync="dialogFormVisible" width="30%">
            <el-form :model="form" label-width="80px" >
                <el-form-item  label="商品参考" >
                    <el-upload
                            class="avatar-uploader"
                            action="http://localhost:9999/file/upload"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item  label="商品名称" >
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item  label="店铺名称" >
                    <el-input v-model="form.firm" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item  label="库存" >
                    <el-input v-model="form.number" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item  label="地址" >
                    <el-input v-model="form.address" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item  label="价格" >
                    <el-input v-model="form.price" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item  label="商品简介" >
                    <el-input type="textarea" v-model="form.introduce" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item  label="商品活动" >
                    <el-input type="textarea" v-model="form.active" autocomplete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="savaFalse">取 消</el-button>
                <el-button type="primary" @click="saveTure">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import request from "@/utils/request";
    export default {
        name: "User.vue",
        data(){
            return{
                pushDialogFormVisible:false,
                userName:this.role=JSON.parse(localStorage.getItem("user")).username,
                pageNum:1,
                pageSize:4,
                tableData:[],
                total:0,
                dialogFormVisible:false,
                form:{

                },
                multipleSelection:[],
                name:"",
                id:"",
                roles:[],
                imageUrl:"",
                role: this.role=JSON.parse(localStorage.getItem("user")).role,
                img:"",
            }
        },
        created() {
            this.load()
        },
        methods:{
            handleAvatarSuccess(res, file) {
                this.form.img=res
                this.imageUrl = URL.createObjectURL(file.raw);
                console.log(this.imageUrl)
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 5;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 5MB!');
                }
                return isJPG && isLt2M;
            },
            load(){
                request.get("/commodity/page",{
                    params:{
                        pageNum:this.pageNum,
                        pageSize:this.pageSize,
                        name:this.name,
                        id:this.id,
                        username:this.userName,
                        role:this.role
                    }
                }).then(res=>{
                    this.tableData=res.records
                    console.log(res.records)
                    this.total=res.total
                    console.log(this.role,this.userName)
                })
                // fetch("http://localhost:9999/user/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize+"&username="+this.username).then(res=>res.json()).then(res=>{
                //   this.tableData=res.data
                //   this.total=res.total
                // })
request.get("/role").then(res=>{
    this.roles=res
})
            },
            handleSizeChange(pageSize){
                this.pageSize=pageSize
                this.load()
            },
            handleCurrentChange(pageNum){
                this.pageNum=pageNum
                this.load()
            },
            save(){
                this.dialogFormVisible = true
                this.form={}
            },
            saveTure(){
this.form.userName = this.userName
                this.form.img=this.imageUrl
                request.post("/commodity",this.form).then(res=>{
                    if(res){
                        this.$message.success("保存成功")
                        this.dialogFormVisible=false
                        this.load()
                        this.imageUrl=""
                    }
                    else {
                        this.$message.error("保存失败")
                        this.dialogFormVisible=false
                        this.load()
                    }

                })
            },
            savaFalse(){
                this.dialogFormVisible=false
                this.form={}
            },
            edit(row){
                this.form=row
                this.dialogFormVisible=true
            },

del(id){
    request.delete("/commodity/"+id).then(res=>{
        if(res){
            this.$message.success("删除成功")

            this.load()
        }
        else {
            this.$message.error("删除失败")

            this.load()
        }
    })
},
            handleSelectionChange(val){
                this.multipleSelection=val
            },
            reset(){
                this.name=""
                this.id=""
                this.load()
            },
            exp(){
                window.open("http://localhost:9999/user/export")
            },
            handleExcelImportSuccess() {
                this.$message.success("导入成功")
                this.load()
            },
            push(row){
                this.form = row
                this.form.commodityId = this.form.id
                this.form.sale=0
                this.form.popularity=0
                delete this.form.id
                request.post("/shop",this.form).then(res => {
                    if(res){
                        this.$message.success("发布成功")
                        this.dialogFormVisible=false
                        this.load()
                        this.imageUrl=""
                    }
                    else {
                        this.$message.error("发布失败")
                        this.dialogFormVisible=false
                        this.load()
                    }
                })
            }

        }
    }

</script>

<style>
    .demo-table-expand {

        margin-left: 30px;
    }
.demo-table-expand label {
    font-size: 15px;
        width: 120px;
        color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 80%;
    }

/*    图片上传样式*/
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader{
        /*margin-left: 30px;*/
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;

    }
</style>